/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_RESOURCEMANAGER_HPP
#define XERN_RESOURCEMANAGER_HPP

#include <string>
#include <map>
#include <set>
#include <boost/shared_ptr.hpp>
#include <boost/uuid/uuid_generators.hpp>

#include "IResource.hpp"
#include "IResourceLoader.hpp"
#include "Hashes.hpp"
#include "ResourceGroup.hpp"

namespace Xern
{
	struct IDataFilter;
	struct IResourceCache;
	struct IResourceListener;
	struct IResourceLoadedCallback;
	class Entity;
	class ResourceManager
	{
	public:
		typedef std::map<std::string, boost::shared_ptr<ResourceGroup> > GroupsType;
		typedef std::pair<boost::weak_ptr<Entity>, boost::weak_ptr<IResource> > UsedResource;
		typedef std::set<UsedResource> UsedResources;

		ResourceManager();
		~ResourceManager();

		boost::shared_ptr<IResourceCache> GetResourceCache();
		void SetResourceCache(boost::shared_ptr<IResourceCache> cache);
		void Shutdown();

		boost::shared_ptr<ResourceGroup> GetGroup(const std::string &name);
		boost::shared_ptr<ResourceGroup> CreateGroup(const std::string &name);
		std::string FindResourcePath(const std::string &name, const std::string &group);

		void LoadGroups(const std::string &path);

		const GroupsType &GetGroups() const;

		void RegisterResourceLoader(boost::shared_ptr<IResourceLoader> loader);
		void UnregisterResourceLoader(boost::shared_ptr<IResourceLoader> loader);

		void RegisterResourceListener(boost::shared_ptr<IResourceListener> listener);
		void UnregisterResourceListener(boost::shared_ptr<IResourceListener> listener);

		void OnAddedResource(boost::shared_ptr<Entity> entity, boost::shared_ptr<IResource> resource);
		void OnRemovedResource(boost::shared_ptr<Entity> entity, boost::shared_ptr<IResource> resource);
		const UsedResources &GetUsedResources() const;

		boost::shared_ptr<IResource> LoadResource(const std::string &filename, const std::string &group);
		boost::shared_ptr<IResource> LoadFromCache(const std::string &filename);
		boost::shared_ptr<IResource> LoadFromCache(const Sha256 &hash);
		boost::shared_ptr<IResource> LoadFromCache(const uuid &id);
		boost::shared_ptr<IResource> LoadFromMemory(const std::string &mime, boost::shared_ptr<IDataFilter> data);
		bool DelayedLoad(const uuid &id, boost::shared_ptr<IResourceLoadedCallback> callback);

		boost::shared_ptr<IResource> FindResource(const uuid &id);
		void RegisterExternalResource(boost::shared_ptr<IResource> resource);
		void RenameResource(boost::shared_ptr<IResource> resource, const uuid &id);

		void UpdateState(boost::shared_ptr<IResource> resource, boost::shared_ptr<IDataFilter> stateData);

		void LoadPlugins();

	private:

		typedef std::set<boost::shared_ptr<IResourceLoader> > loadersType;
		typedef std::set<boost::shared_ptr<IResourceListener> > listenersType;
		typedef std::map<uuid, boost::weak_ptr<IResource> > resourceMapType;

		UsedResources usedResources;
		loadersType loaders;
		listenersType listeners;
		GroupsType groups;
		resourceMapType resourceMap;
		boost::shared_ptr<IResourceCache> resourceCache;
		boost::uuids::random_generator idGenerator;
	};
}; // namespace Xern

#endif //XERN_RESOURCEMANAGER_HPP
